package cn.edu.zzuli.web.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import cn.edu.zzuli.search.SearchIndexBean;
import cn.edu.zzuli.web.Constant;
import cn.edu.zzuli.web.bean.Forum;
import cn.edu.zzuli.web.controller.model.ForumForm;
import cn.edu.zzuli.web.dao.base.PageView;
import cn.edu.zzuli.web.dao.base.QueryResult;

@Controller
public class ForumController extends AbstractController {

	/**
	 * 论坛帖子列表页面
	 */
	@RequestMapping(value = "/forum/list", method = RequestMethod.GET)
	public String forumList(HttpServletRequest request, HttpServletResponse response) {
		
		int currentPage = 0;
		String c = request.getParameter("currentPage");
		if(!StringUtils.isBlank(c) && StringUtils.isNumeric(c))
			currentPage = Integer.parseInt(c);
		
		if(currentPage <= 0) 
			currentPage = 1;
		
		int record = this.getForumDao().getTotalForum();
		List<Forum> list = this.getForumDao().getForumByPage(Constant.PAGE_SIZE, currentPage);
		
		PageView<Forum> pageView = new PageView<Forum>(Constant.PAGE_SIZE, currentPage);
		QueryResult<Forum> qr = new QueryResult<Forum>();
		qr.setResultlist(list);
		qr.setTotalrecord(record);
		pageView.setQueryResult(qr);
		request.setAttribute("pageView", pageView);
		
		return "/forum/list";
	}
	
	/**
	 * 查看帖子页面
	 */
	@RequestMapping(value = "/forum/view", method = RequestMethod.GET)
	public String forumView(HttpServletRequest request, HttpServletResponse response ,int id) {
		
		Forum forum = this.getForumDao().getForumByRowCallbackHandler(id);
		
		int currentPage = 0;
		String c = request.getParameter("currentPage");//当前页
		//当前页数字的判断
		if(!StringUtils.isBlank(c) && StringUtils.isNumeric(c))
			currentPage = Integer.parseInt(c);
		if(currentPage <= 0) 
			currentPage = 1;
		
		int record = this.getForumDao().getTotalListForum(id);
		List<Forum> list = this.getForumDao().getForumListByPage(Constant.PAGE_SIZE, currentPage ,id);
		
		PageView<Forum> pageView = new PageView<Forum>(Constant.PAGE_SIZE, currentPage);
		QueryResult<Forum> qr = new QueryResult<Forum>();
		qr.setResultlist(list);
		qr.setTotalrecord(record);
		pageView.setQueryResult(qr);
		request.setAttribute("forum", forum);
		request.setAttribute("pageView", pageView);
		
		return "/forum/view";
	}
	
	@RequestMapping(value = "/forum/delete", method = RequestMethod.GET)
	public String forumDelete (HttpServletRequest request, HttpServletResponse response ,int id) {
		
		Forum forum = this.getForumDao().getForumByRowCallbackHandler(id);
		this.getForumDao().deleteForum(forum.getId());
		return "redirect:/forum/manager.html";
	}
	
	/**
	 * 论坛发表帖子页面
	 */
	@RequestMapping(value = "/forum/addreply", method = RequestMethod.POST)
	public String forumAddReply(HttpServletRequest request, HttpServletResponse response ,
			ForumForm form, BindingResult result) throws Exception{
		
		Forum forum = new Forum();
		BeanUtils.copyProperties(forum, form);
		forum.setAuthor(request.getUserPrincipal().getName());
		this.getForumDao().insert(forum);
		return "redirect:/forum/view.html?id=" + forum.getPid() ;
	}
	
	/**
	 * 论坛发表帖子页面
	 */
	@RequestMapping(value = "/forum/add", method = RequestMethod.GET)
	public String forumAddUI() {
		return "/forum/add";
	}
	
	/**
	 * 论坛发表帖子页面
	 */
	@RequestMapping(value = "/forum/add", method = RequestMethod.POST)
	public String forumAdd(HttpServletRequest request, HttpServletResponse response ,
			ForumForm form, BindingResult result) throws Exception{
		
		String username = request.getUserPrincipal().getName();
		Forum forum = new Forum();
		BeanUtils.copyProperties(forum, form);
		forum.setAuthor(username);
		this.getForumDao().insert(forum);
		return "redirect:/forum/list.html";
	}
	
	/**
	 * 论坛管理页面
	 */
	@RequestMapping(value = "/forum/manager", method = RequestMethod.GET)
	public String forumManager(HttpServletRequest request, HttpServletResponse response) {
		
		int currentPage = 0;
		String c = request.getParameter("currentPage");
		if(!StringUtils.isBlank(c) && StringUtils.isNumeric(c))
			currentPage = Integer.parseInt(c);
		
		if(currentPage <= 0) 
			currentPage = 1;
		
		int record = this.getForumDao().getTotalForum();
		List<Forum> list = this.getForumDao().getForumByPage(Constant.PAGE_SIZE, currentPage);
		
		PageView<Forum> pageView = new PageView<Forum>(Constant.PAGE_SIZE, currentPage);
		QueryResult<Forum> qr = new QueryResult<Forum>();
		qr.setResultlist(list);
		qr.setTotalrecord(record);
		pageView.setQueryResult(qr);
		request.setAttribute("pageView", pageView);
		
		return "/forum/manager";
	}
	
	/**
	 * 论坛帖子全文搜索页面
	 */
	@RequestMapping(value = "/forum/search", method = RequestMethod.GET)
	public String forumSearchUI() {
		return "/forum/search";
	}
	
	/**
	 * 论坛帖子全文搜索页面
	 */
	@RequestMapping(value = "/forum/search", method = RequestMethod.POST)
	public String forumSearch(HttpServletRequest request, HttpServletResponse response ,String search) {
		System.out.println("查询关键字:::" + search);
		SearchIndexBean t = new SearchIndexBean(Constant.SEARCH_INDEX_DIR);
		t.setMultiField(search);
		t.setSort("id", true, 1);
		List<Forum> list = t.getCustomersSort();
		List<Integer> intList = new ArrayList<Integer>();
		for (int i = 0; i < list.size(); i++) {
			try {
				Forum forum = (Forum) list.get(i);
				intList.add(forum.getId());
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		if(intList.size() > 0){
			List<Forum> flist = this.getForumDao().getForumByID(intList);
			request.setAttribute("pageView", flist);
		}
		return "/forum/search";
	}
	
	/**
	 * 论坛帖子全文搜索页面
	 */
	@RequestMapping(value = "/forum/createindex", method = RequestMethod.GET)
	public String createindex() {
		this.getCreateIndex().create();
		return "redirect:/forum/manager.html";
	}
	
	@RequestMapping(value = "/forum/deleteUnnecessary", method = RequestMethod.GET)
	public String deleteUnnecessary() {
		this.getForumDao().deleteUnnecessary();
		return "redirect:/forum/manager.html";
	}
	
}
